package hk.m4s.lr.repair.test.ui.equipment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.DeviceModel;
import hk.m4s.lr.repair.test.model.DeviceModelResult;
import hk.m4s.lr.repair.test.service.device.DeviceListervice;
import hk.m4s.lr.repair.test.ui.adapter.BaoYangListAdapter;
import hk.m4s.lr.repair.test.ui.adapter.EquipmentListAdapter;
import hk.m4s.lr.repair.test.ui.user.UserNoticeActivity;
import hk.m4s.lr.repair.test.ui.webview.WebViewActivity;
import hk.m4s.lr.repair.test.views.listview.VListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/3/22.
 * 设备台账
 */

public class MyEquipmentActivity extends UeBaseActivity implements VListView.IXListViewListener{
    private VListView vlist;
    private List<DeviceModel> list=new ArrayList<DeviceModel>();
    private EquipmentListAdapter equipmentListAdapter;
    private TextView tipsText;
    private Context context;
    private int pageNum = 1;
    private int pageSize = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.acyivity_equipment_list);
        context=this;
        hiddenFooter();
        showGoBackBtn();
        setTitleText("我负责的设备");
        findBaseView();
    }
    private void findBaseView() {
        vlist=(VListView)findViewById(R.id.vlist);
        tipsText=(TextView)findViewById(R.id.tipsText);
        vlist.setXListViewListener(this);
        vlist.setOnItemClickListener(clickItem);
        equipmentListAdapter=new EquipmentListAdapter(context,list);
        vlist.setAdapter(equipmentListAdapter);
        getData("1");
    }
    private void onLoad() {
        vlist.stopRefresh();
        vlist.stopLoadMore();
        Date data = new Date(System.currentTimeMillis());
        vlist.setRefreshTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.CHINA).format(data));
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub
        // getData();
        vlist.setPullLoadEnable(true);
        pageNum=0;
        getData("1");
    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub
        vlist.setPullRefreshEnable(true);
        pageNum++;
         getData("2");
    }
    public void getData(final String refreshType){
        Map<String, Object> parmas = new HashMap<>();
        parmas.put("device_type", Constant.device_type);
        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
        try {
            JSONObject jsondata = new JSONObject();
            jsondata.put("pageNum", pageNum);
            jsondata.put("pageSize", pageSize);
            //jsondata.put("noPage", "1");
            parmas.put("jsonText",jsondata.toString());
        }catch (JSONException e){

        }
        DeviceListervice.myDeviceList(context,parmas , new ResponseCallback<DeviceModelResult>() {
            @Override
            public void onSuccess(DeviceModelResult deviceModelResult) {
                onLoad();
                switch (refreshType) {
                    case "1":
                        list.clear();
                        if(deviceModelResult.getList()!=null) {
                            tipsText.setVisibility(View.GONE);
                            list.addAll(deviceModelResult.getList());
                            if (deviceModelResult.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            } else {
                                vlist.showFooterView();
                                vlist.setPullLoadEnable(true);
                            }
                        }else{
                            tipsText.setVisibility(View.VISIBLE);
                        }
                        break;
                    case "2":
                        if(deviceModelResult.getList()!=null) {
                            list.addAll(deviceModelResult.getList());
                            if (deviceModelResult.getList().size() <= pageSize) {
                                vlist.removeFooterView();
                                vlist.setPullLoadEnable(false);
                            }
                        }
                        break;
                }
                equipmentListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
                tipsText.setVisibility(View.VISIBLE);
            }
        });
    }
    AdapterView.OnItemClickListener clickItem = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            DeviceModel deviceModel=list.get(arg2-1);
            Intent aboutIntent=new Intent(MyEquipmentActivity.this, MyEquipmentDetailActivity.class);
            aboutIntent.putExtra("deviceModel",deviceModel);
            aboutIntent.putExtra("show","2");
            startActivity(aboutIntent);
        }

    };
}
